package com.shawn.springboot.integeration.netty.client.handler;

import com.shawn.springboot.integeration.netty.future.SyncWriteCache;
import com.shawn.springboot.integeration.netty.future.SyncWriteFuture;
import com.shawn.springboot.integeration.netty.msg.Response;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zhangjian
 * @title: AbstractChannelHandler
 * @projectName 12-springboot学习
 * @description: 业务处理
 * @date 2021/4/2715:01
 */
@Slf4j
public abstract class AbstractChannelHandler extends SimpleChannelInboundHandler<Response> {
    public abstract void doDealData(Response res);
    @Override
    protected void channelRead0(ChannelHandlerContext chc, Response res) {
        SyncWriteFuture future = SyncWriteCache.FUTURE_CACHE.getIfPresent(res.getRequestId());
        if (null == future) {
            doDealData(res);
            return;
        }
        future.setResponse(res);
    }
    @Override
    public void channelActive(ChannelHandlerContext chx) {
        log.debug("[{}]连接成功", chx.channel().remoteAddress());
    }
    @Override
    public void channelInactive(ChannelHandlerContext chx) {
        log.info("与[{}]连接断开", chx.channel().remoteAddress());
    }
}
